﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DMS.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DMS.Base;

namespace DMS.Business.BaseClass
{
    public class BaseService
    {
        private DMSEntities _db;
        /// <summary>
        /// 数据库对象模型
        /// </summary>
        public DMSEntities DB
        {
            get
            {
                if (_db == null)
                    _db = new DMSEntities();
               return _db;
            }
            set
            {
                _db = new DMSEntities();
            }
        }

        public BaseService()
        {
        }

        /// <summary>
        /// 由外部控制数据库模型
        /// </summary>
        /// <param name="db"></param>
        public BaseService(DMSEntities db)
        {
            this.DB = db;
        }

        /// <summary>
        /// 释放数据库对象模型
        /// </summary>
        public void DisposeDB()
        {
            DB.Dispose();
        }

        protected static int GetRecordCount(string sql, List<DbParameter> dbParams)
        {
            Database db = DBHelper.Instance;
            DbCommand command = db.GetSqlStringCommand(sql);
            if (dbParams != null && dbParams.Count > 0)
            {
                foreach (DbParameter param in dbParams)
                {
                    DbParameter p = command.CreateParameter();

                    p.Direction = param.Direction;
                    p.DbType = param.DbType;

                    p.ParameterName = param.ParameterName;
                    p.Value = param.Value;

                    command.Parameters.Add(p);

                }
            }

            return Convert.ToInt32(db.ExecuteScalar(command));
        }
    }
}
